package com.yanqu.road.server.cmd.activity.summergames;

import com.yanqu.road.entity.activity.summergames.SummerGamesConfig;
import com.yanqu.road.entity.activity.summergames.SummerGamesUserData;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.SummerGamesProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.CmdApi;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.summergames.SummerGamesModule;
import com.yanqu.road.server.gameplayer.module.player.BagModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.summergames.SummerGamesActivityMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;

@Cmd(code = Protocol.S_SUMMER_GAMES_EXCHANGE, desc = "同意交换")
@CmdApi(reqMsg = SummerGamesProto.SummerGamesExchangeAgreeReqMsg.class, respMsg = SummerGamesProto.SummerGamesExchangeAgreeRespMsg.class)
public class SummerGamesExchangeActionCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {


        SummerGamesConfig config = SummerGamesActivityMgr.getSummerGamesConfig();
        // 没有活动
        if (config == null || !ActivityMgr.activityInTimeAndShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }

        SummerGamesUserData userData = player.getModule(SummerGamesModule.class).getUserData();
        if(userData == null){
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }

        SummerGamesProto.SummerGamesExchangeAgreeReqMsg reqMsg = SummerGamesProto.SummerGamesExchangeAgreeReqMsg.parseFrom(packet.getBytes());
        GoodsInfo goodsInfo = GoodsMgr.getGoodsById(reqMsg.getMedalsId());
        if(goodsInfo == null){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }
        Property cost = PropertyHelper.parseStringToProperty(reqMsg.getMedalsId() + "=1");
        if(!player.getModule(CurrencyModule.class).currencyIsEnough(cost)){
            ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
            return;
        }

        long count = player.getModule(BagModule.class).getUserBagItemCount(reqMsg.getMedalsId());
        if(count < config.getSUMMERGAME_EXCHANGE_NUM_LIMIT()){
            ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
            return;
        }

        player.getModule(PreReduceModule.class).preReduce(PropertyHelper.parseGoodsToString(reqMsg.getMedalsId(), BigInteger.ONE));

        SummerGamesProto.CrossSummerGamesExchangeAgreeReqMsg.Builder crossReq = SummerGamesProto.CrossSummerGamesExchangeAgreeReqMsg.newBuilder();
        crossReq.setActivityId(config.getActivityId());
        crossReq.setId(reqMsg.getId());
        crossReq.setMedalsId(reqMsg.getMedalsId());
        crossReq.setUnionUid(player.getUserInfo().getUnionUid());
        player.sendPacket(Protocol.C_SUMMER_GAMES_EXCHANGE_AGREE, crossReq);

    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            player.sendPacket(Protocol.U_SUMMER_GAMES_EXCHANGE, SummerGamesProto.SummerGamesExchangeAgreeRespMsg.newBuilder().setRet(ret));
        }
    }
}
